package com.fourhats.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fourhats.exception.NotAllowedAnonymousException;
import com.fourhats.exception.UserAsksOwnProductException;
import com.fourhats.exception.UserNotOwerOfProductException;
import com.fourhats.model.ObjectTypes;
import com.fourhats.services.EmailService;
import com.fourhats.services.ProductService;
import com.fourhats.services.QuestionService;
import com.fourhats.services.UserService;
import com.fourhats.services.dto.AnswerDto;
import com.fourhats.services.dto.ProductDto;
import com.fourhats.services.dto.QuestionDto;
import com.fourhats.services.util.Page;

@Controller
public class QuestionController extends GenericController {

	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private EmailService emailService;
	
	@Autowired
	private UserService userService;
	
	@Value("#{configProps}")
	private Properties properties;
	
	@RequestMapping(value="/question/askProduct")
	public @ResponseBody QuestionDto askProduct(long productId, String question) throws NotAllowedAnonymousException, UserAsksOwnProductException {
		ProductDto product = this.productService.getProductById(productId);
		if(this.getUserId() == product.getUserId()) {
			throw new UserAsksOwnProductException();
		}
		
		QuestionDto questionDto = questionService.createQuestion(question, this.getUserId(), productId, ObjectTypes.PRODUCT.getValue());
		Map<String, String> emailValues = new HashMap<String, String>();
		emailValues.put("{UserName}", product.getUserName());
		emailValues.put("{ProductName}", product.getName());
		
		try {
			this.emailService.sendEMail(product.getMarketEmail(), "Te han hecho una pregunta sobre un producto sustentable", this.properties.getProperty("folder.emails"), "newQuestionEmail", emailValues);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return questionDto;
	}
	
	@RequestMapping(value="/question/askMarket")
	public @ResponseBody QuestionDto askMarket(long marketId, String question) throws NotAllowedAnonymousException {
		return questionService.createQuestion(question, this.getUserId(), marketId, ObjectTypes.MARKET.getValue());
	}
	
	@RequestMapping(value="/question/answerProductQuestion")
	public @ResponseBody AnswerDto answerProductQuestion(long questionId, String answer) throws NotAllowedAnonymousException, UserNotOwerOfProductException {
		QuestionDto question = questionService.getQuestionById(questionId);
		ProductDto product = this.productService.getProductById(question.getQuestionableObject().getEntityId());
		
		if(this.getUserId() != product.getUserId()) {
			throw new UserNotOwerOfProductException();
		}
		
		AnswerDto answerDto = questionService.answerQuestion(answer, this.getUserId(), questionId);
		Map<String, String> emailValues = new HashMap<String, String>();
		emailValues.put("{UserName}", question.getUserName());
		emailValues.put("{ProductName}", product.getName());
		
		try {
			this.emailService.sendEMail(question.getUserEmail(), "Te han respondido la pregunta sobre un producto sustentable", this.properties.getProperty("folder.emails"), "newAnswerEmail", emailValues);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return answerDto;
	}
	
	@RequestMapping(value="/question/canAsk",
	method = RequestMethod.GET, 
	headers="Accept=*/*")
	public @ResponseBody String canAsk() {
		super.verifyAuthentication();
		return super.SUCCESS_MESSAGE;
	}
	
	@RequestMapping(value="/question/getQuestionsToUser")
	public @ResponseBody Page<QuestionDto> getQuestionsToUser(int pageNumber, int pageSize){
		return questionService.getQuestionsToUser(getUserId(), pageNumber, pageSize);
	}
	
	@RequestMapping(value="/question/getQuestionsMadeByUser")
	public @ResponseBody Page<QuestionDto> getQuestionsMadeByUser(int pageNumber, int pageSize){
		return questionService.getQuestionsMadeByUser(getUserId(), pageNumber, pageSize);
	}
}
